package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    NursingPlanMapper nursingPlanMapper;
    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public List<NursingPlanVo> fingAll() {
        List<NursingPlanVo> list= nursingPlanMapper.findAll();
        return list;
    }

    @Override
    public PageBean<NursingPlanVo> query(NursingPlanPageQueryDto dto) {

        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<NursingPlanVo> vo =nursingPlanMapper.query(dto);
        return PageBean.of(vo,NursingPlanVo.class);
    }

    @Override
    public void add(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        //nursingPlan.setCreateTime(LocalDateTime.now());
        //nursingPlan.setUpdateTime(LocalDateTime.now());
        //nursingPlan.setCreateBy(1671403256519078138L);
        //nursingPlan.setUpdateBy(1671403256519078138L);
        nursingPlan.setStatus(1);
        nursingPlanMapper.add(nursingPlan);

        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        List<NursingProjectPlan> list = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);


        list.forEach(projectPlan->{
            projectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.add(projectPlan);
        });

    }

    @Override
    public NursingPlanVo getById(Integer id) {
        NursingPlan plan = nursingPlanMapper.getById(id);
        List<NursingProjectPlanVo> vos= nursingProjectPlanMapper.getByPlanId(id);
        NursingPlanVo planVo = BeanUtil.toBean(plan, NursingPlanVo.class);
        planVo.setProjectPlans(vos);
        return planVo;
    }

    @Override
    public void update(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        //nursingPlan.setUpdateTime(LocalDateTime.now());
        //nursingPlan.setUpdateBy(1671403256519078138L);
        nursingPlanMapper.update(nursingPlan);

        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());

        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        List<NursingProjectPlan> list = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);


        list.forEach(projectPlan->{
            projectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.add(projectPlan);
        });

    }

    @Override
    public void deleteid(Integer id) {
        NursingPlan byId = nursingPlanMapper.getById(id);
        if (byId.getStatus()==1){
            throw new BaseException(BasicEnum.DELETE_PLAN_STATUS_FAIL);
        }
        List<NursingProjectPlanVo> byPlanId = nursingProjectPlanMapper.getByPlanId(id);
        if (CollUtil.isEmpty(byPlanId)){
            throw new BaseException(BasicEnum.DELETE_PLAN_FAIL);
        }
        nursingPlanMapper.deleteid(id);
    }

    @Override
    public void updatestatus(Long id, Integer status) {
        NursingPlan build = NursingPlan.builder().id(id).status(status).build();
        nursingPlanMapper.update(build);
    }


}
